﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UFC.DBUtility;
using System.Data;
using System.IO;
using System.Threading.Tasks;

namespace MESQualityHistoryRecord
{
   public class DeleteWorkFLowFile
    {
       public void Excute()
       {
           try {
               //查询所有置为无效的工艺下的附件信息path
               var sqlStr = @"select distinct o.filepath,wr.workflowid from workflowmodifyrecord wr 
                        left join workflowstep ws on ws.workflowid =wr.workflowid 
                        left join specbase sb on sb.specbaseid=ws.specbaseid
                        left join spec s on s.specid=nvl(sb.revofrcdid,ws.specid)
                        left join objectdocinfo o on o.parentid =wr.workflowid or o.parentid =s.specid 
                        where wr.islastdate <>2 and o.filepath is not null 
                    ";

               DataTable dt = OracleHelper.QueryTable(sqlStr,null);
               if(dt.Rows.Count>0)
               {
                   Parallel.ForEach(dt.Select(), item =>
                    {
                        var path =@"C:\Program Files (x86)\Camstar\InSite Web Application\InSiteWebApplication\"+ item["filepath"].ToString();
                        if (File.Exists(path))
                        {
                            File.Delete(path);
                        }
                        OracleHelper.ExecuteSql(string.Format("update workflowmodifyrecord set islastdate=2 where workflowid='{0}'", item["workflowid"].ToString()));
                    });
               }
           
           }catch(Exception ex){
               UFC.Common.LogHelp.WriteLog("工艺附件唯一性删除异常："+ex.Message);
           }
       }
    }
}
